package com.example.user.coolrun.runnershign;

import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.net.Uri;
import android.os.Handler;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;
import com.example.user.coolrun.R;

import java.io.IOException;

import static com.example.user.coolrun.R.raw.ys;

public class Menu extends Activity {

    private Toast loadMessage;
    private Runnable gameLauncher;
    private Intent gameIntent;
    private Handler mHandler;
    MediaPlayer mus;
    private SoundPool soundPool;
    private int soundID;
    private android.widget.ImageButton play;
    private android.widget.ImageButton stop;


    @SuppressLint("WrongViewCast")
    @Override
    public void onCreate(Bundle savedInstanceState) {

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        mus = MediaPlayer.create(this, R.raw.ys);
        play = (android.widget.ImageButton)findViewById(R.id.bofangButton);
        stop = (android.widget.ImageButton)findViewById(R.id.stopButton);
        stop.setVisibility(View.VISIBLE);
        play.setVisibility(View.GONE);

        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stop.setVisibility(View.VISIBLE);
                if (mus.isPlaying()){
                    mus.pause();
                }
                play.setVisibility(View.GONE);
            }
        });

        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                play.setVisibility(View.VISIBLE);
                if (!mus.isPlaying()){
                    mus.start();
                }
                stop.setVisibility(View.GONE);
            }
        });
        initSound();


        ImageView ben = (ImageView)findViewById(R.id.ben);
        ImageView pao = (ImageView)findViewById(R.id.pao);
        ImageView ba = (ImageView)findViewById(R.id.ba);
        ImageView dian = (ImageView)findViewById(R.id.dian);
        ImageView ke = (ImageView)findViewById(R.id.ke);
        ImageView yuan = (ImageView)findViewById(R.id.yuan);

        ObjectAnimator translationAnim1 = ObjectAnimator.ofFloat(ben, "translationX", 2f, 800f,0f);
        translationAnim1.setDuration(3000);
        translationAnim1.start();
        ObjectAnimator rotationAnim1 = ObjectAnimator.ofFloat(ben, "rotationY", 9f, 360f);
        rotationAnim1.setDuration(3500);
        rotationAnim1.start();
        ObjectAnimator alphaAnim1 = ObjectAnimator.ofFloat(ben, "alpha", 0f, 3f);
        alphaAnim1.setDuration(8000);
        alphaAnim1.start();
        ObjectAnimator scaleAnim1 = ObjectAnimator.ofFloat(ben, "scaleX", 1f, 1.2f, 1f);
        scaleAnim1.setDuration(4000);
        scaleAnim1.start();
        ObjectAnimator scaleAnim2 = ObjectAnimator.ofFloat(ben, "scaleY", 1f, 1.2f, 1f);
        scaleAnim2.setDuration(4000);
        scaleAnim2.start();

        ObjectAnimator translationAnim2 = ObjectAnimator.ofFloat(pao, "translationX", 2f, 800f,0f);
        translationAnim2.setDuration(3000);
        translationAnim2.start();
        ObjectAnimator rotationAnim2 = ObjectAnimator.ofFloat(pao, "rotationY", 9f, 360f);
        rotationAnim2.setDuration(3500);
        rotationAnim2.start();
        ObjectAnimator alphaAnim2 = ObjectAnimator.ofFloat(pao, "alpha", 0f, 3f);
        alphaAnim2.setDuration(8000);
        alphaAnim2.start();
        ObjectAnimator scaleAnim3 = ObjectAnimator.ofFloat(pao, "scaleX", 1f, 1.2f, 1f);
        scaleAnim3.setDuration(4000);
        scaleAnim3.start();
        ObjectAnimator scaleAnim4 = ObjectAnimator.ofFloat(pao, "scaleY", 1f, 1.2f, 1f);
        scaleAnim4.setDuration(4000);
        scaleAnim4.start();

        ObjectAnimator translationAnim3 = ObjectAnimator.ofFloat(ba, "translationX", 2f, 800f,0f);
        translationAnim3.setDuration(3000);
        translationAnim3.start();
        ObjectAnimator rotationAnim3 = ObjectAnimator.ofFloat(ba, "rotationY", 9f, 360f);
        rotationAnim3.setDuration(3500);
        rotationAnim3.start();
        ObjectAnimator alphaAnim3 = ObjectAnimator.ofFloat(ba, "alpha", 0f, 3f);
        alphaAnim3.setDuration(8000);
        alphaAnim3.start();
        ObjectAnimator scaleAnim5 = ObjectAnimator.ofFloat(ba, "scaleX", 1f, 1.2f, 1f);
        scaleAnim5.setDuration(4000);
        scaleAnim5.start();
        ObjectAnimator scaleAnim6 = ObjectAnimator.ofFloat(ba, "scaleY", 1f, 1.2f, 1f);
        scaleAnim6.setDuration(4000);
        scaleAnim6.start();

        ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(dian, "translationX", 2f, 80f,0f);
        objectAnimator1.setDuration(3000);
        objectAnimator1.start();
        ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(dian, "rotationX", 9f, 360f);
        objectAnimator2.setDuration(3500);
        objectAnimator2.start();
        ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(dian, "rotationY", 9f, 360f);
        objectAnimator3.setDuration(3500);
        objectAnimator3.start();
        ObjectAnimator objectAnimator4 = ObjectAnimator.ofFloat(dian, "alpha", 0f, 3f);
        objectAnimator4.setDuration(8000);
        objectAnimator4.start();
        ObjectAnimator objectAnimator5 = ObjectAnimator.ofFloat(dian, "scaleX", 1f, 1.1f, 1f);
        objectAnimator5.setDuration(4000);
        objectAnimator5.start();
        ObjectAnimator objectAnimator6 = ObjectAnimator.ofFloat(dian, "scaleY", 1f, 1.1f, 1f);
        objectAnimator6.setDuration(4000);
        objectAnimator6.start();

        ObjectAnimator objectAnimator7 = ObjectAnimator.ofFloat(ke, "translationX", 2f, 80f,0f);
        objectAnimator7.setDuration(3000);
        objectAnimator7.start();
        ObjectAnimator objectAnimator8 = ObjectAnimator.ofFloat(ke, "rotationX", 9f, 360f);
        objectAnimator8.setDuration(3500);
        objectAnimator8.start();
        ObjectAnimator objectAnimator9 = ObjectAnimator.ofFloat(ke, "rotationY", 9f, 360f);
        objectAnimator9.setDuration(3500);
        objectAnimator9.start();
        ObjectAnimator objectAnimator10 = ObjectAnimator.ofFloat(ke, "alpha", 0f, 3f);
        objectAnimator10.setDuration(8000);
        objectAnimator10.start();
        ObjectAnimator objectAnimator11 = ObjectAnimator.ofFloat(ke, "scaleX", 1f, 1.1f, 1f);
        objectAnimator11.setDuration(4000);
        objectAnimator11.start();
        ObjectAnimator objectAnimator12 = ObjectAnimator.ofFloat(ke, "scaleY", 1f, 1.1f, 1f);
        objectAnimator12.setDuration(4000);
        objectAnimator12.start();

        ObjectAnimator objectAnimator13 = ObjectAnimator.ofFloat(yuan, "translationX", 2f, 80f,0f);
        objectAnimator13.setDuration(3000);
        objectAnimator13.start();
        ObjectAnimator objectAnimator14 = ObjectAnimator.ofFloat(yuan, "rotationX", 9f, 360f);
        objectAnimator14.setDuration(3500);
        objectAnimator14.start();
        ObjectAnimator objectAnimator15 = ObjectAnimator.ofFloat(yuan, "rotationY", 9f, 360f);
        objectAnimator15.setDuration(3500);
        objectAnimator15.start();
        ObjectAnimator objectAnimator16 = ObjectAnimator.ofFloat(yuan, "alpha", 0f, 3f);
        objectAnimator16.setDuration(8000);
        objectAnimator16.start();
        ObjectAnimator objectAnimator17 = ObjectAnimator.ofFloat(yuan, "scaleX", 1f, 1.1f, 1f);
        objectAnimator17.setDuration(4000);
        objectAnimator17.start();
        ObjectAnimator objectAnimator18 = ObjectAnimator.ofFloat(yuan, "scaleY", 1f, 1.1f, 1f);
        objectAnimator18.setDuration(4000);
        objectAnimator18.start();

        loadMessage = Toast.makeText(getApplicationContext(), "游戏加载中···", Toast.LENGTH_SHORT );
        loadMessage.setGravity(Gravity.CENTER|Gravity.CENTER, 0, 0);

        gameIntent = new Intent (this, main.class);
        gameLauncher = new Runnable() {

            public void run() {
                startActivityForResult(gameIntent, 0);
            }
        };

        mHandler = new Handler();


    }


    protected void onResume() {
        if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
        super.onResume();
//        if (!mus.isPlaying() || play.getVisibility() != View.VISIBLE)
//            mus.start();
//            play.setVisibility(View.VISIBLE);
//            stop.setVisibility(View.GONE);

        if(!mus.isPlaying())
            play.setVisibility(View.GONE);
            stop.setVisibility(View.VISIBLE);
    }

    public void playGame(View view) {
        // Loading Toast
        mus.pause();
        loadMessage.show();
        Settings.SHOW_FPS = false;
        mHandler.post(gameLauncher);
    }

    public void showScore(View view) {
        mus.pause();
        Intent myIntent = new Intent (this, HighScoreActivity.class);
        startActivity (myIntent);
    }

    public void showShop(View view) {
        mus.pause();
        Intent myIntent = new Intent (this, ShopActivity.class);
        startActivity (myIntent);
    }

    public void showInstruction(View view) {
        mus.pause();
        Intent myIntent = new Intent (this, SetActivity.class);
        startActivity (myIntent);
    }

    public void exit(View view) {
        android.support.v7.app.AlertDialog.Builder dialog = new android.support.v7.app.AlertDialog.Builder(Menu.this);
        dialog.setTitle("提醒");
        dialog.setMessage("你要离开游戏了吗？");
        dialog.setCancelable(false);
        dialog.setNegativeButton("再玩一会", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        dialog.setPositiveButton("狠心离开", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mus.release();
                finish();
            }
        });
        dialog.show();
    }


    protected Dialog onCreateDialog(int id) {
        return new AlertDialog.Builder(this)
                .setTitle("Error while changing view")
                .setMessage("System needs some time to free memory. Please try again in 10 seconds.")
                .setCancelable(true)
                .create();
    }

    @SuppressLint("NewApi")
    private void initSound() {
        soundPool = new SoundPool(0,AudioManager.STREAM_MUSIC,0);
        soundID = soundPool.load(this,R.raw.beng,1);
    }

    private void playSound(){
        soundPool.play(soundID,0.1f,0.5f,0,0,0);
    }

    private void exit() {

        android.support.v7.app.AlertDialog.Builder dialog = new android.support.v7.app.AlertDialog.Builder(Menu.this);
        dialog.setTitle("提醒");
        dialog.setMessage("你要离开游戏了吗？");
        dialog.setCancelable(false);
        dialog.setNegativeButton("再玩一会", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        dialog.setPositiveButton("狠心离开", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        dialog.show();

    }
}

